package com.android.clinic.app.ui.doctor;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.android.clinic.app.R;
import com.android.clinic.app.base.BaseActivity;
import com.android.clinic.app.entity.DepartmentInfo;
import com.android.clinic.app.http.HttpClient;
import com.android.clinic.app.http.base.BaseObserver;
import com.android.clinic.app.http.utils.RxObservableUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by cheng on 2018/2/11.
 * 科室分类
 */
public class DepartmentClassifyActivity extends BaseActivity {

    private ListView listViewOne, listViewTwo;
    private ClassifyOneAdapter oneAdapter;
    private ClassifyTwoAdapter twoAdapter;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_department_classify;
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        String labelName = this.getIntent().getStringExtra("labelName");
        String departIDd = this.getIntent().getStringExtra("departId");
        setTitleBarTitle(labelName);
        setTitleBarLeftResource(R.drawable.icon_back);

        listViewOne = (ListView) findViewById(R.id.listView_one);
        listViewTwo = (ListView) findViewById(R.id.listView_two);

        ArrayList<DepartmentInfo> classifyInfoList = (ArrayList<DepartmentInfo>) this.getIntent().getSerializableExtra("classifyList");

        oneAdapter = new ClassifyOneAdapter();
        oneAdapter.setClassifyInfoList(classifyInfoList);
        oneAdapter.setSelectName(labelName);
        listViewOne.setAdapter(oneAdapter);
        getSecondDepartment(departIDd);

        listViewOne.setOnItemClickListener((parent, view, position, id) -> {
            oneAdapter.setSelectName(oneAdapter.getItem(position).getName());
            getSecondDepartment(oneAdapter.getItem(position).getId());
        });

        listViewTwo.setOnItemClickListener((parent, view, position, id) -> {
            Intent intent = new Intent(DepartmentClassifyActivity.this, ListAllDoctorActivity.class);
            intent.putExtra("deptName", twoAdapter.getItem(position).getName());
            startActivity(intent);
        });
    }

    private void getSecondDepartment(String departId) {
        HttpClient.getHttpService().getSecondDepartment(departId)
                .compose(this.bindToLifecycle())
                .compose(RxObservableUtils.applySchedulers())
                .subscribe(new BaseObserver<List<DepartmentInfo>>() {
                    @Override
                    public void onSuccess(List<DepartmentInfo> departmentInfo) {
                        twoAdapter = new ClassifyTwoAdapter();
                        twoAdapter.setClassifyInfoList(departmentInfo);
                        listViewTwo.setAdapter(twoAdapter);
                    }

                    @Override
                    public void onFailure(String message) {

                    }
                });
    }

    private class ClassifyOneAdapter extends BaseAdapter {

        private ArrayList<DepartmentInfo> classifyInfoList;
        private String selectName = "";

        public void setClassifyInfoList(ArrayList<DepartmentInfo> classifyInfoList) {
            this.classifyInfoList = classifyInfoList;
        }

        public String getSelectName() {
            return selectName;
        }

        public void setSelectName(String selectName) {
            this.selectName = selectName;
            notifyDataSetChanged();
        }

        @Override
        public int getCount() {
            return classifyInfoList.size();
        }

        @Override
        public DepartmentInfo getItem(int position) {
            return classifyInfoList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (null == convertView) {
                convertView = getLayoutInflater().inflate(R.layout.item_department_classify, null);
                holder = new ViewHolder();
                holder.tvName = (TextView) convertView.findViewById(R.id.tv_name);
                holder.layLabel = (LinearLayout) convertView.findViewById(R.id.layout_label);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.tvName.setText(getItem(position).getName());
            if (getSelectName().equals(getItem(position).getName())) {
                holder.layLabel.setBackgroundColor(Color.parseColor("#FFFFFF"));
            } else {
                holder.layLabel.setBackgroundColor(Color.parseColor("#E9EEF2"));
            }
            return convertView;
        }
    }

    private class ClassifyTwoAdapter extends BaseAdapter {

        private List<DepartmentInfo> classifyInfoList;

        public void setClassifyInfoList(List<DepartmentInfo> classifyInfoList) {
            this.classifyInfoList = classifyInfoList;
        }

        @Override
        public int getCount() {
            return classifyInfoList.size();
        }

        @Override
        public DepartmentInfo getItem(int position) {
            return classifyInfoList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (null == convertView) {
                convertView = getLayoutInflater().inflate(R.layout.item_department_classify, null);
                holder = new ViewHolder();
                holder.tvName = (TextView) convertView.findViewById(R.id.tv_name);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.tvName.setText(getItem(position).getName());
            return convertView;
        }

    }

    class ViewHolder {
        TextView tvName;
        LinearLayout layLabel;
    }
}
